package org.yn.mkt.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.yn.emp.domain.Employee;
import org.yn.mkt.domain.Activity;
import org.yn.mkt.domain.ActivityRemark;
import org.yn.mkt.dto.ActivityDTO;
import org.yn.mkt.mapper.ActivityMapper;
import org.yn.mkt.mapper.ActivityRemarkMapper;
import org.yn.mkt.mapper.ClueActivityMapper;
import org.yn.mkt.service.IActivityService;
import org.yn.base.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yn
 * @since 2023-07-31
 */
@Service
public class ActivityServiceImpl extends BaseServiceImpl<Activity> implements IActivityService {

    @Autowired
    private ActivityRemarkMapper mapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    ClueActivityMapper clueActivityMapper;

    @Override
    public void insert(Activity activity, Employee loginUser) {
        super.insert(activity);
        String content = "%s创建了活动%s";
        activityRemark(activity,String.format(content,loginUser.getUsername(),activity.getName()));
    }

    @Override
    public void update(Activity activity, Employee loginUser) {
        super.update(activity);
        String content = "%s修改了活动%s";
        activityRemark(activity,String.format(content,loginUser.getUsername(),activity.getName()));
    }

    @Override
    public void delete(Long id) {
        Activity activity = super.selectById(id);
        super.delete(id); // 以后基本都是走的 逻辑删除
        String content = "%s删除了活动%s";
        activityRemark(activity,String.format(content,"员工",activity.getName()));
    }


    public void activityRemark(Activity activity,String content){
        // 创建备注对象
        ActivityRemark activityRemark = new ActivityRemark();
        activityRemark.setCreateTime(new Date());
        activityRemark.setCreateBy(1L);
        activityRemark.setNoteContent(content);
        activityRemark.setActivityId(activity.getId());
        mapper.insert(activityRemark);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        super.batchDelete(ids);
    }

    @Override
    public void activityBindClue(ActivityDTO activityDTO) {
        clueActivityMapper.delete(activityDTO.getClueId());
        activityMapper.activityBindClue(activityDTO);
    }
}
